package com.black.quick.controller;

import com.black.quick.domain.User;
import com.black.quick.redis.RedisService;
import com.black.quick.redis.UserKey;
import com.black.quick.result.Result;
import com.black.quick.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author shy_black
 * @date 2019/7/23 8:59
 * @Description:
 */
@Controller
@RequestMapping("/demo")
public class SampleController {

    @Autowired
    UserService userService;
    @Autowired
    RedisService redisService;

//    @RequestMapping("/redis/get")
//    @ResponseBody
//    public Result<Boolean> redisGet(){
//        User user = new User();
//        user.setId(1);
//        user.setName("1111");
//        redisService.get(UserKey.getById, ""+1, User.class);
//        return Result.success(true);
//    }

    @RequestMapping("/redis/get")
    @ResponseBody
    public Result<User> redisGet() {
        User  user  = redisService.get(UserKey.getById, ""+2, User.class);
        return Result.success(user);
    }

    @RequestMapping("/redis/set")
    @ResponseBody
    public Result<Boolean> redisSet() {
        User user  = new User();
        user.setId(2);
        user.setName("2222");
        redisService.set(UserKey.getById, ""+2, user);//UserKey:id1
        return Result.success(true);
    }

    @RequestMapping("/db/tx")
    @ResponseBody
    public Result<Boolean> dbTx() {
        userService.tx();
        return Result.success(true);
    }

    @RequestMapping("/db/get")
    @ResponseBody
    public Result<User> dbget() {
        User user = userService.getById(1);
        return Result.success(user);
    }

    @RequestMapping("/thymelaf/hello")
    public String thymeleaf(Model model) {
        model.addAttribute("name","black");
        return "hello";
    }
}
